package com.teach.controller.photo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.teach.dto.PhotoPopularAuthorDTO;
import com.teach.entity.UserInfo;
import com.teach.service.impl.UserServiceImpl;
import com.teach.service.impl.photo.PhotoPicServiceImpl;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/photo")
@Slf4j
public class PhotoPopularAuthorController {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

	@Autowired
	private PhotoPicServiceImpl impl;
	@Autowired
	private UserServiceImpl userServiceImpl;

	@RequestMapping("popular-author.do")
	public Object popularAuthor() {
		List<UserInfo> infos = userServiceImpl.selectAll();
		List<PhotoPopularAuthorDTO> list = new ArrayList<>();
		for (UserInfo userInfo : infos) {
			if (impl.selectWorksCount(sdf.format(new Date()) + "%", userInfo.getUserName()) != 0) {
				 PhotoPopularAuthorDTO authorDTO = impl.selectPopularAuthor(sdf.format(new Date()) + "%", userInfo.getUserName());
				 log.debug("authorDTO = {}", authorDTO);
				 log.debug("authorDTO=====================================================>");
				list.add(authorDTO);
			}

		}
		log.debug("=====================================================>");
		for (PhotoPopularAuthorDTO map2 : list) {
			log.debug("PhotoPopularAuthorDTO = {}", map2);
		}
		log.debug("=====================================================>");
//		// 对List按点赞数倒序
		list.sort(new Comparator<PhotoPopularAuthorDTO>() {

			@Override
			public int compare(PhotoPopularAuthorDTO o1, PhotoPopularAuthorDTO o2) {
				// TODO Auto-generated method stub
				return o2.getPhotoPicGreatCount().compareTo(o1.getPhotoPicGreatCount());
			}
		});
		log.debug("=====================================>");
		log.debug("=====================================================>");
		for (PhotoPopularAuthorDTO map2 : list) {
			log.debug("PhotoPopularAuthorDTO = {}", map2);
		}
		log.debug("=====================================================>");
		if (list.size()>5) {
			return list.subList(0, 5);
		}
		return list;
	}
}
